/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/converter/StatusMaskConverter.hpp"

StatusMaskConverter* StatusMaskConverter::singleton_ = nullptr;

StatusMaskConverter::StatusMaskConverter() {}

StatusMaskConverter* StatusMaskConverter::GetInstance()
{
    if (singleton_ == nullptr) {
        singleton_ = new StatusMaskConverter();
    }
    return singleton_;
}

dal_ret_t StatusMaskConverter::ConvertStatusMaskFromDalToRtidds(dal_status_mask_t d_mask,
                                                                DDS_StatusMask* r_mask)
{
    *r_mask = 0;
    uint32_t dds_all_mask = DDS_INCONSISTENT_TOPIC_STATUS | DDS_PUBLICATION_MATCHED_STATUS |
                            DDS_LIVELINESS_LOST_STATUS | DDS_OFFERED_DEADLINE_MISSED_STATUS |
                            DDS_OFFERED_INCOMPATIBLE_QOS_STATUS | DDS_SUBSCRIPTION_MATCHED_STATUS |
                            DDS_DATA_ON_READERS_STATUS | DDS_SAMPLE_LOST_STATUS |
                            DDS_DATA_AVAILABLE_STATUS | DDS_SAMPLE_REJECTED_STATUS |
                            DDS_LIVELINESS_CHANGED_STATUS | DDS_REQUESTED_DEADLINE_MISSED_STATUS |
                            DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS;

    if (d_mask == DAL_ANY_STATUS) {
        *r_mask = dds_all_mask;
        return DAL_RETCODE_OK;
    }

    if (d_mask & DAL_INCONSISTENT_TOPIC_STATUS) {
        *r_mask |= DDS_INCONSISTENT_TOPIC_STATUS;
    }
    if (d_mask & DAL_OFFERED_DEADLINE_MISSED_STATUS) {
        *r_mask |= DDS_OFFERED_DEADLINE_MISSED_STATUS;
    }
    if (d_mask & DAL_REQUESTED_DEADLINE_MISSED_STATUS) {
        *r_mask |= DDS_REQUESTED_DEADLINE_MISSED_STATUS;
    }
    if (d_mask & DAL_OFFERED_INCOMPATIBLE_QOS_STATUS) {
        *r_mask |= DDS_OFFERED_INCOMPATIBLE_QOS_STATUS;
    }
    if (d_mask & DAL_REQUESTED_INCOMPATIBLE_QOS_STATUS) {
        *r_mask |= DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS;
    }
    if (d_mask & DAL_SAMPLE_LOST_STATUS) {
        *r_mask |= DDS_SAMPLE_LOST_STATUS;
    }
    if (d_mask & DAL_SAMPLE_REJECTED_STATUS) {
        *r_mask |= DDS_SAMPLE_REJECTED_STATUS;
    }
    if (d_mask & DAL_DATA_ON_READERS_STATUS) {
        *r_mask |= DDS_DATA_ON_READERS_STATUS;
    }
    if (d_mask & DAL_DATA_AVAILABLE_STATUS) {
        *r_mask |= DDS_DATA_AVAILABLE_STATUS;
    }
    if (d_mask & DAL_LIVELINESS_LOST_STATUS) {
        *r_mask |= DDS_LIVELINESS_LOST_STATUS;
    }
    if (d_mask & DAL_LIVELINESS_CHANGED_STATUS) {
        *r_mask |= DDS_LIVELINESS_CHANGED_STATUS;
    }
    if (d_mask & DAL_PUBLICATION_MATCHED_STATUS) {
        *r_mask |= DDS_PUBLICATION_MATCHED_STATUS;
    }
    if (d_mask & DAL_SUBSCRIPTION_MATCHED_STATUS) {
        *r_mask |= DDS_SUBSCRIPTION_MATCHED_STATUS;
    }

    return DAL_RETCODE_OK;
}